#!/usr/bin/python
'''
Filter a masker output file to a given list of regions.
'''
import sys
import os

# parameters.
filter_file = sys.argv[1]

# get list of files or directories.
inputs = []
for i in range(2,len(sys.argv)):
	inputs.append(sys.argv[i])

# read in filter.
filters = {}
fin = open(filter_file, "rb")
for line in fin:
	filters[line.strip().replace('"Motif:','').replace('"','')] = True
fin.close()

# get list of files.
filter_files = []
for input in inputs:
	if os.path.isdir(input) == True:
		for f in os.listdir(input):
			filter_files.append("%s/%s" % (input, f))
	else:
		filter_files.append(input)
	

# loop over dirs.
for in_file in filter_files:
	# stream sam file.
	fin = open(in_file, "rb")
	i = 0
	for line in fin: 
		# skips.
		i += 1
		if i <= 3: continue
		
		# get id.
		tmp = line.split()
		id = tmp[9]
		
		# check against filter.
		if id not in filters:
			print line.strip()

	fin.close()
